package momo.app.util;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class IntByteConvert
{
	public static final byte[] intToByteArray(int value)
	{
		return new byte[] {
				(byte)(value >>> 24),
				(byte)(value >>> 16),
				(byte)(value >>> 8),
				(byte)value};
	}
	
	public static final int byteArrayToInt(byte [] b)
	{
		return (b[0] << 24)
			+ ((b[1] & 0xFF) << 16)
			+ ((b[2] & 0xFF) << 8)
			+ (b[3] & 0xFF);
	}
	
	public static final byte[] dataToByteArray(String data)
	{
		//Conversion based on UTF-8
		byte[] strByte = data.getBytes(Charset.forName("UTF-8"));
		int byteLen = strByte.length;
		
		ByteBuffer buffer = ByteBuffer.allocate(byteLen + 4);
		buffer.putInt(byteLen);
		buffer.put(strByte);
		
		return buffer.array();
	}

}//end of class IntByteConvert
